package com.jinxin.platform.cdcardpassmgr.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2021-02-05
 * @Author: Lingnan
 **/
public class DateUtil {

    public static final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    //毫秒转LocalDateTime
    public static LocalDateTime getDateTimeOfTimestamp(long timestamp) {
        Instant instant = Instant.ofEpochMilli(timestamp);
        ZoneId zone = ZoneId.systemDefault();
        return LocalDateTime.ofInstant(instant, zone);
    }

    public static String getLastDay(){
        LocalDate nowDate = LocalDate.now();
        LocalDate yesterDay = nowDate.minusDays(1);
        return yesterDay.toString();
    }

    public static LocalDateTime timeString2LocalDateTime(String timeStr,String pattern){
        DateTimeFormatter df = DateTimeFormatter.ofPattern(pattern);
        return LocalDateTime.parse(timeStr,df);
    }
    public static String getFirstDayOfCurrentYear(){
        Calendar seekedDate = Calendar.getInstance();
        int year = seekedDate.get(Calendar.YEAR);
        return year + "-01-01 00:00:00"; // 2021-01-01 00:00:00
    }

    public static String getFirstDayOfCurrentMonth(){
        Calendar seekedDate = Calendar.getInstance();
        int year = seekedDate.get(Calendar.YEAR);
        int month = seekedDate.get(Calendar.MONTH) + 1;
        return year + "-"+month+"-01 00:00:00"; // 2021-01-01 00:00:00
    }

    public static String getCurrentDayYMDHHs() {
        return df.format(new Date());
    }

    public static String[] getFirstLastDayOfMonth(int year, int month)
    {
        String [] result =  new String[2];
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month-1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd ");
        result[1] = format.format(calendar.getTime()) + " 23:59:59";
        calendar.set(Calendar.DATE, calendar.getActualMinimum(Calendar.DATE));
        result[0] = format.format(calendar.getTime()) + " 00:00:00";
        return result;
    }
    public static int getCurrentField(int field){
        Calendar calendar = Calendar.getInstance();
        if(field == Calendar.MONTH){
            return calendar.get(Calendar.MONTH) + 1;
        }else {
            return calendar.get(field);
        }
    }


    public static void main(String[] args) {
        System.out.println(getCurrentField(Calendar.YEAR));
//        String[] timeArray = getFirstLastDayOfMonth(2020, 2);
//        System.out.println(timeArray[0]);
//        System.out.println(timeArray[1]);
//        System.out.println(getFirstDayOfCurrentMonth());
//        System.out.println(getCurrentDayYMDHHs());
//        System.out.println(getFirstDayOfCurrentYear());;
//        System.out.println(timeString2LocalDateTime("20201113164745","yyyyMMddHHmmss"));
    }
}
